<?php
/**
 * Created by PhpStorm.
 * User: sunku
 * Date: 2017/12/20
 * Time: 16:42
 */

namespace Admin\Controller;
use Admin\Controller\BaseController;

class EnquiryController extends BaseController
{
    /**
     * 构造函数
     */
    public function __construct() {
        //无需进行权限检测的功能
        \Org\Auth\AuthUtil::setNoAuthFuncList([
            '/admin/enquiry/ajaxSaveEnquiry',
            '/admin/enquiry/ajaxExistItem'
        ]);
        parent::__construct();
    }

    public function enquiryList(){
        $name = I("get.name");
        $status = I("get.status",0);
        //分页
        $page = I('get.page', 1);
        $pagesize = 15;

        $params = array();
        $params['page'] = $page;
        $params['page_size'] = $pagesize;
        $name && $params['name'] = $name;
        $status && $params['status'] = $status;

        $result = D('Resource/Enquiry', 'Service')->searchEnquiryList($params);

        $page = $result['page'];
        $page_size = $result['page_size'];
        $total = $result['total'];
        $total_page = $result['total_page'];
        $enquiryList = $result['dataList'];

        //生成分页
        $url = "/enquiry/enquiryList?name={$name}&status={$status}";
        $page_html = create_page($url, $total, $page, $page_size);

        $this->assign('page', $page);
        $this->assign('page_size', $page_size);
        $this->assign('total', $total);
        $this->assign('total_page', $total_page);
        $this->assign('page_html', $page_html);

        $this->assign('name', $name);
        $this->assign('status', $status);

        $this->assign('enquiryList', $enquiryList);

        $this->loadFrame('enquiryList');
    }
    /**
     * 编辑
     */
    public function editEnquiry(){
        $id = I("get.id");
        $enquiryInfo = [];
        if($id){
            $enquiryInfo = D("Enquiry","Logic")->getEnquiryInfo(['id'=>$id]);
        }
        $enquiryItem = D('Item','Logic')->getAllItemList(['is_enquiry'=>1,'status'=>1]);
        $this->assign('enquiryItem', $enquiryItem);
        $this->assign('enquiryInfo', $enquiryInfo);
        $this->assign('enquiry_id', $id);

        $this->loadFrame('editEnquiry');
    }

    /**
     * @return string
     */
    public function ajaxSaveEnquiry(){
        $data = [];
        isset($_POST['id']) && $data['id'] = intval($_POST['id']);
        isset($_POST['name']) && $data['name'] = strval($_POST['name']);
        isset($_POST['status']) && $data['status'] = intval($_POST['status']);
        isset($_POST['item_ids']) && $data['item_ids'] = strval($_POST['item_ids']);
        isset($_POST['detail']) && $data['detail'] = $_POST['detail'];
        if(!$data){
            return Response(201,'无需操作');
        }
        D('Enquiry','Logic')->saveEnquiry($data);
    }

    /**
     * @return string
     */
    public function ajaxExistItem(){
        $id = I('post.id');
        $item_id = I('post.item_id');
        if(!$item_id){
           return Response(2001,"参数异常");
        }
        $info = D("Resource/Enquiry","Service")->getItemEnquiryBySql(['item_id'=>$item_id]);

        if(!$info){
           return Response(999,"请求成功");
        }else{
            if($id && ($info['0']['id']==$id)){
                return Response(999,"请求成功");
            }
            return Response(2002,"该产品已添加询价模板");
        }
    }

    /**
     * @return string
     */
    public function ajaxDeleteEnquiry(){
        $id = I('post.id');
        $status = I('post.status');

        if(!$id){
            return Response(2001,"参数异常");
        }
        $data=[];
        $data['id'] = $id;
        $data['status'] = $status;

        D("Enquiry","Logic")->deleteEnquiry($data);
    }
}